
package Database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


public class account {
     public static boolean vali_login(String id, String pass, String type)
    {
        boolean re=false;
        try {
            PreparedStatement pt = Connect.Connect.Con().prepareStatement("select * from Admin where account=? and pass=? and [type]=?");
            pt.setString(1, id);
            pt.setString(2, pass);
            pt.setString(3, type);
            ResultSet set=pt.executeQuery();
            if(set.next()==true)
            {
                re=true;
            }
            set.close();
            Connect.Connect.Con().close();
        } catch (Exception ex) {
            Logger.getLogger(account.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }
    public static ArrayList getaccid()
    {
        ArrayList list=new ArrayList();
        try {
            ResultSet set = Connect.Connect.Con().createStatement().executeQuery("select AdminID from Admin");
            while(set.next())
            {
                list.add(set.getObject(1));
            }
            set.close();
            Connect.Connect.Con().close();
        } catch (Exception ex) {
            Logger.getLogger(account.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }
     public static ResultSet getaccdetail(String id)
    {
        ResultSet set=null;
        try {
            set = Connect.Connect.Con().createStatement().executeQuery("select * from Admin where AdminID="+id);
        } catch (Exception ex) {
            Logger.getLogger(account.class.getName()).log(Level.SEVERE, null, ex);
        }
        return set;
    }
     public static boolean addnew(String user, String pass, String note, String type)
    {
        boolean re=false;
        try {
            PreparedStatement st = Connect.Connect.Con().prepareStatement("insert into Admin values(?,?,?,?)");
            st.setString(1, user);
            st.setString(2, pass);
            st.setString(3, note);
            st.setString(4, type);
            int res=st.executeUpdate();
            if(res!=0)
            {
                re=true;
            }
            st.close();
            Connect.Connect.Con().close();
        } catch (Exception ex) {
            Logger.getLogger(account.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }
       public static boolean delete(String id)
    {
        boolean res=false;
        PreparedStatement st;
        try {
            st = Connect.Connect.Con().prepareStatement("delete from Admin where AdminID=?");
            st.setString(1, id);
            int re=st.executeUpdate();
            if(re!=0)
            {
                res=true;
            }
            st.close();
            Connect.Connect.Con().close();
        } catch (Exception ex) {
            Logger.getLogger(account.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }
       public static boolean edit(String id, String pass, String note, String type)
    {
        boolean re = false;
        try {
            PreparedStatement st = Connect.Connect.Con().prepareStatement("update Admin set pass=?, note=?,[type]=? where AdminID=?");
            st.setString(1, pass);
            st.setString(2, note);
            st.setString(3, type);
            st.setString(4, id);
            int res = st.executeUpdate();
            if(res!=0)
            {
                re=true;
            }
            st.close();
            Connect.Connect.Con().close();
        } catch (Exception ex) {
            Logger.getLogger(account.class.getName()).log(Level.SEVERE, null, ex);
        }
        return re;
    }
}
